home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / SASETUP.MSI / F77539_inc_global.js < prev    next >
Encoding:
JavaScript  |  2003-02-21  |  11.5 KB  |  431 lines

  1.     //------------------------------------------------------------------------
  2.     // 
  3.     //    inc_global.js:        Resuable  JavaScript functions 
  4.     //                        used accross all the pages
  5.     //
  6.     // Copyright (c) Microsoft Corporation.  All rights reserved.
  7.     //
  8.     //    Date             Description
  9.     //  25/07/2000        Created date
  10.     //------------------------------------------------------------------------
  11.         
  12.     //------------------------------------------------------------------------
  13.     // Function to clear the error messages (if any on screen) whenever required
  14.     //------------------------------------------------------------------------
  15.     function ClearErr()
  16.     { 
  17.         // checking for the browser type 
  18.         if (IsIE()) 
  19.         {
  20.             var oDiv = document.all("divErrMsg");
  21.             if ( oDiv ) oDiv.innerHTML = "";
  22.             // removing the event handling
  23.             document.frmTask.onkeypress = null ;
  24.         }
  25.     }    
  26.     //------------------------------------------------------------------------
  27.        // Function:    To check if given input is INTEGER or not
  28.     // input:        Text value, text length
  29.     // returns:        True if the field is integer else false
  30.     //------------------------------------------------------------------------
  31.     function isInteger(strText)
  32.     {
  33.          var blnResult = true;
  34.          var strChar;
  35.          // checking for null string 
  36.          if (strText.length==0)  
  37.          {
  38.             blnResult=false;
  39.          }
  40.          
  41.          for(var i=0;i < strText.length;i++)
  42.          {
  43.             strChar=strText.substring(i,i+1);
  44.             if(strChar < "0" || strChar > "9")
  45.             { 
  46.               blnResult = false;
  47.             }
  48.          } 
  49.          return blnResult;
  50.     }
  51.     //------------------------------------------------------------------------
  52.     // Function:    getRadioButtonValue
  53.     // Description: Get's the selected radioButton value
  54.     // input:        Object    -Radio Object
  55.     // returns:        String    -value of the selected radio Button
  56.     //------------------------------------------------------------------------
  57.     function getRadioButtonValue(objRadio)
  58.     {
  59.         var strValue;
  60.         for(var i =0; i < objRadio.length; i++)
  61.         { 
  62.              //checking for If selected
  63.             if(objRadio[i].checked)
  64.             {
  65.                 strValue = objRadio[i].value ;
  66.                 break;
  67.             }
  68.         }
  69.         return strValue;
  70.     }
  71.     
  72.     //------------------------------------------------------------------------
  73.     // Function:    To count the number of occurences of given character in the text
  74.     // input:        strText-sourceString
  75.     //        :        charToCount-character to be checked 
  76.     // returns:        The count of no of character
  77.     //------------------------------------------------------------------------
  78.     function countChars(strText,charToCount)
  79.     {
  80.         var intStartingPosition = 0;
  81.         var intFoundPosition =0;
  82.         var intCount = 0;
  83.         
  84.         // checking for the null character    
  85.         if (charToCount=="")
  86.         {
  87.             return intCount;
  88.         }    
  89.         while((intFoundPosition=strText.indexOf(charToCount,intStartingPosition)) >= 0)
  90.         {
  91.             intCount++;
  92.             intStartingPosition = intFoundPosition + 1;
  93.         }
  94.             
  95.         return intCount ;
  96.     }
  97.     
  98.     //------------------------------------------------------------------------
  99.     // Function:    Check to see if all characters in string are spaces
  100.     // input:    strText-sourceString
  101.     // returns:
  102.     //  0 - not all spaces
  103.     //  1 - all spaces
  104.     //------------------------------------------------------------------------
  105.     function IsAllSpaces(strText)
  106.     {
  107.         var bIsAllSpaces;
  108.  
  109.         if (countChars(strText," ") == strText.length)
  110.         {
  111.             bIsAllSpaces = 1;
  112.         }    
  113.         else
  114.         {
  115.             bIsAllSpaces = 0;
  116.         }
  117.             
  118.         return bIsAllSpaces ;
  119.     }
  120.     
  121.     //------------------------------------------------------------------------
  122.     // Function name:    selectFocus
  123.     // Description:        select and focus on the Textbox object 
  124.     // input:            The object on which focus must be set
  125.     //------------------------------------------------------------------------
  126.     function selectFocus(objControl)
  127.     {
  128.         objControl.focus();
  129.         objControl.select();
  130.     }
  131.     //------------------------------------------------------------------------
  132.     // Function :   removeListBoxItems
  133.     // Description: To remove the selected options from the given list
  134.     //                the selected items in the list object is removed from 
  135.     //                the list on click of Remove button and sets focus on IP 
  136.     //                address text object or the Remove button object depending
  137.     //                 on conditions
  138.     // Input:        objList -Listbox
  139.     //        :        btnRemove -Button object for disable/enable
  140.     // Returns:
  141.     // Support functions used :
  142.     //        ClearErr
  143.     //------------------------------------------------------------------------
  144.     function removeListBoxItems(objList,btnRemove)
  145.     {
  146.         // Clear any previous error messages
  147.         ClearErr();
  148.         var i=0;
  149.         // number of elements in the list object
  150.         var intListLength = objList.length ;
  151.         var intDeletedItemPosition
  152.                 
  153.         while(i < intListLength)
  154.         {
  155.             if ( objList.options[i].selected )
  156.             {
  157.                 intDeletedItemPosition = i
  158.                 objList.options[i]=null;                
  159.                     
  160.                 intListLength=objList.length;
  161.             }
  162.             else 
  163.                 i++;    
  164.         }   
  165.         if (intDeletedItemPosition >=objList.length)
  166.             intDeletedItemPosition = intDeletedItemPosition -1
  167.                 
  168.         if(objList.length == 0)
  169.         {
  170.             btnRemove.disabled = true;
  171.             //
  172.             btnRemove.value = btnRemove.value;
  173.             
  174.         }    
  175.         else
  176.         {
  177.             objList.options[intDeletedItemPosition].selected = true;
  178.             // focus on the Remove button
  179.             btnRemove.focus();            
  180.         }    
  181.     }
  182.     
  183.     //------------------------------------------------------------------------
  184.     // Function:    addToListBox
  185.     // Description:    moves the passed textbox value to ListBox
  186.     // input:        objList-List Object 
  187.     //        :        ButtonObject- Remove button
  188.     //        :        strText-Text of the option item
  189.     //        :        strValue-value of the option item
  190.     // output:        btnRemove-Button      
  191.     //------------------------------------------------------------------------
  192.     function addToListBox(objList,btnRemove,strText,strValue)
  193.     {
  194.         var blnResult=true;
  195.         // checking for the text value null  
  196.         // If the value passed is null make it as text 
  197.         if (strValue=="")
  198.         {
  199.             strValue=strText;
  200.         }     
  201.         if (strText!="" )
  202.         {
  203.             // check for duplicates not required as duplicates accepted
  204.             if (!chkDuplicate(objList,strText))
  205.             {
  206.                 // create a new option in the list box
  207.                 objList.options[objList.length] = new Option(strText,strValue);
  208.                     
  209.                 objList.options[objList.length-1].selected = true;                
  210.                 // enable the Remove button
  211.                 if(btnRemove.disabled)
  212.                     btnRemove.disabled = false ;
  213.             }
  214.             else
  215.             {
  216.                 blnResult= false;
  217.             }    
  218.         }
  219.         else
  220.         {
  221.             blnResult= false;
  222.         }                
  223.         return blnResult;
  224.     }
  225.     
  226.     //------------------------------------------------------------------------
  227.     // Function:    chkDuplicate        
  228.     // Description: checks for the duplicate text in the list box
  229.     // input:        Object        -Radio Object
  230.     //         :        strchkName    -value of the Name to be checked
  231.     //returns:        blnDuplicate-Returns true/false on success/failure 
  232.     //------------------------------------------------------------------------
  233.     function chkDuplicate(objList,strchkName)
  234.     {
  235.         var i;
  236.         var blnDuplicate=false;
  237.         for(var i=0;i < objList.length;i++)
  238.         {
  239.             if (objList.options[i].text == strchkName)
  240.                 blnDuplicate = true;
  241.         }
  242.         return blnDuplicate;
  243.     }
  244.     
  245.     //------------------------------------------------------------------------
  246.     // Function:        isValidIP
  247.     // Description:        to validate the IP address
  248.     // input:            IP address text object
  249.     // returns:0 if it is valid 
  250.     // 1    Empty
  251.     // 2    Invalid Format, number of dots is not 3
  252.     // 3    non-integers present in the value
  253.     // 4    start ip > 223
  254.     // 5    Should not start with 127
  255.     // 6    out of bound
  256.     // 7    All zeros
  257.     // 8    Should not be 0        
  258.     // support functions: 
  259.     //        IsAllSpaces
  260.     //        countChars
  261.     //        isInteger
  262.     //------------------------------------------------------------------------
  263.     function isValidIP(objIP) 
  264.     {
  265.         var strIPtext = objIP.value; 
  266.         if ((strIPtext.length == 0) || IsAllSpaces(strIPtext)) 
  267.         { 
  268.             // IP Empty
  269.             return 1;
  270.         }
  271.         
  272.         if ( countChars(strIPtext,".") != 3) 
  273.         { 
  274.             // Invalid Format, number of dots is not 3
  275.             return 2;
  276.         }
  277.         var arrIP = strIPtext.split(".");
  278.             
  279.         for(var i = 0; i < 4; i++)
  280.         {
  281.             if ( (arrIP[i].length < 1 ) || (arrIP[i].length > 3 ) )
  282.             {
  283.                 // Invalid Format, continuous dots or more than 3 digits given between dots
  284.                 return 2;
  285.             }
  286.                 
  287.             if ( !isInteger(arrIP[i]) )
  288.             {
  289.                 // non-integers present in the value
  290.                 return 3;
  291.             }
  292.                 
  293.             arrIP[i] = parseInt(arrIP[i]);
  294.                 
  295.             if(i == 0)
  296.             {
  297.                 // start IP value
  298.                 if(arrIP[i] == 0)
  299.                 {
  300.                     // start IP value must not be 0
  301.                     return 8;
  302.                 }
  303.  
  304.                 if(arrIP[i] > 223)
  305.                 {
  306.                     // start IP must not be > 223
  307.                     return 4;
  308.                 }
  309.                 if(arrIP[i] == 127)
  310.                 {
  311.                     // start IP must not be 127 - Loopback ip
  312.                     return 5;
  313.                 }
  314.             }
  315.             else
  316.             {
  317.                 // the 2nd, 3rd and 4th IP values between the dots
  318.                 // these must not be more than 255
  319.                 if (arrIP[i] > 255)
  320.                 {
  321.                     // IP out of bound
  322.                     return 6;
  323.                 }
  324.             }
  325.         }
  326.             
  327.         objIP.value = arrIP.join(".");
  328.             
  329.         if(objIP.value == "0.0.0.0")
  330.         {
  331.             // IP all zeros
  332.             return 7;
  333.         }    
  334.             
  335.         return 0;
  336.             
  337.     }    // end of isValidIP
  338.     
  339.     //------------------------------------------------------------------------
  340.     // Function        :checkkeyforIPAddress
  341.     // Description    :function to allow only dots and numbers
  342.     // input        :Object    -TextBox Object
  343.     // returns        :none
  344.     //------------------------------------------------------------------------
  345.     function checkKeyforIPAddress(obj)
  346.     {
  347.         // Clear any previous error messages
  348.         ClearErr();
  349.         if (!(window.event.keyCode >=48  && window.event.keyCode <=57 || window.event.keyCode == 46))
  350.         {
  351.             window.event.keyCode = 0;
  352.             obj.focus();
  353.         }
  354.     }
  355.     //------------------------------------------------------------------------
  356.     // Function        :checkkeyforNumbers
  357.     // Description    :function to allow only numbers 
  358.     // input        :Object    -TextBox Object
  359.     // returns        :none
  360.     //------------------------------------------------------------------------
  361.     function checkKeyforNumbers(obj)
  362.     {
  363.         // Clear any previous error messages
  364.         ClearErr();
  365.         if (!(window.event.keyCode >=48  && window.event.keyCode <=57))
  366.         {
  367.             window.event.keyCode = 0;
  368.             obj.focus();
  369.         }
  370.     }
  371.     //------------------------------------------------------------------------
  372.     // Function        :disableAddButton
  373.     // Description    :Function to make the add button disable
  374.     // input        :Object    -TextBox Object
  375.     //                :Object-Addbutton
  376.     // returns        :none
  377.     //------------------------------------------------------------------------
  378.     // Function to make the add button disable
  379.     function disableAddButton(objText,objButton)
  380.     {
  381.         if(Trim(objText.value)=="")
  382.             {
  383.             objButton.disabled=true;
  384.             objButton.value = objButton.value;
  385.             }
  386.         else
  387.             objButton.disabled=false;
  388.     }
  389.     
  390.     
  391.     //------------------------------------------------------------------------
  392.     // Function        :isvalidchar
  393.     // Description    :Function to check whether the input is valid or not
  394.     // input        :Invalid char list
  395.     //                 The input string    
  396.     // returns        :true if it doesnt contain the invalid chars; else false 
  397.     //------------------------------------------------------------------------    
  398.     // Checks For Invalid Key Entry
  399.     function isvalidchar(strInvalidChars,strInput)
  400.     {
  401.         var rc = true;
  402.  
  403.         try 
  404.         {
  405.             var exp = new RegExp(strInvalidChars);
  406.             var result = exp.test(strInput);
  407.  
  408.             if ( result == true )
  409.             {
  410.                 rc = false;
  411.             }
  412.             else
  413.             {
  414.                 rc = true;
  415.             }
  416.  
  417.         }
  418.         catch(oException)
  419.         {
  420.             if ( SA_IsDebugEnabled() )
  421.             {
  422.                 alert("Unexpected exception encountered in function: isvalidchar\n\n"
  423.                         + "Number: " + oException.number + "\n"
  424.                         + "Description: " + oException.description);
  425.             }
  426.         }
  427.         
  428.         return rc;
  429.     }
  430.  
  431.